package com.atguigu.tingshu.payment.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/***
 * 开启可靠性投递-》回调函数
 */
@Log4j2
@Component
public class paymentCallbackConfig implements RabbitTemplate.ReturnsCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init (){
        rabbitTemplate.setReturnsCallback(this::returnedMessage);
    }

    /**
     * 可靠性投递
     * @param returnedMessage
     */
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        log.error("支付结果发送失败，失败的内容是：" +
                new String(returnedMessage.getMessage().getBody())+"交换机是："+
                returnedMessage.getExchange() +"rountingdkey是："+
                returnedMessage.getRoutingKey());
    }
}
